package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.CryptoUtil;
import com.zhongkai.app.service.GuardService;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Guard;
import com.zhongkai.model3.Maintain;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 维修用户
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class GuardServiceImpl implements GuardService {


    @Autowired
    private IBaseDao iBaseDao;


    @Autowired
    private PageQuerDao pageQuerDao;

    /**
     * 添加维修端用户
     *
     * @param uuid      用户一标识
     * @param gua_sex   性别
     * @param gua_name  姓名
     * @param gua_user  用户名
     * @param gua_phone 电话
     * @param gua_pass  密码
     * @param gua_state 状态 1在职 2请假 3离职
     */
    @Override
    public ZhongkaiResult addGuard(String uuid, String gua_sex, String gua_name, String gua_user, String gua_phone, String gua_pass, String gua_state) {

        if (StringUtils.isNullOrEmpty(gua_sex) || StringUtils.isNullOrEmpty(gua_name)
                || StringUtils.isNullOrEmpty(gua_user) || StringUtils.isNullOrEmpty(gua_phone)
                || StringUtils.isNullOrEmpty(gua_pass) || StringUtils.isNullOrEmpty(gua_state)

                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Guard guard;

        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaUser='" + gua_user + "' or gd.guaPhone='" + gua_phone + "'");
        if (guard != null) {
            return ZhongkaiResult.build(400, "账号或手机号已存在");
        }


        guard = new Guard();
        guard.setGuaUuid(uuid);
        guard.setGuaSex(gua_sex);
        guard.setGuaName(gua_name);
        guard.setGuaUser(gua_user);
        guard.setGuaPhone(gua_phone);
        guard.setGuaImg("http://111.61.64.62:8001/group1/M00/00/01/wKgZhV3krh2AaPVDAAQqKpbszHg957.jpg");
        //加密
        String pass = "";
        try {
            pass = CryptoUtil.encode64(CryptoUtil.SALT, gua_pass);

            //  chinaset.setPassWord(pass); //设置加密后的密码
            guard.setGuaPass(pass);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "数据加密失败");
        }


        guard.setGuaState(gua_state);

        long millis = new DateTime().getMillis(); //获取当前系统时间戳

        guard.setGuaTime(millis);

        guard.setGuaRemove("1");

        guard.setGuaLevel(1); //初始等级为1
//........................................................................
/*
        Section section;
        section = (Section) iBaseDao.get("from Section se where se.secName like '%维修%'");

        if (section == null) {
            return ZhongkaiResult.build(400, "请添加维修部门");
        }
*/


        Maintain maintain = new Maintain();
        maintain.setTainUuid(uuid);
        maintain.setTainName(gua_name);
        maintain.setTainSex(gua_sex);
        maintain.setTainPhone(gua_phone);
        maintain.setTainStatus("1"); // 1 在职  2 离职 3请假
        maintain.setTainTime(millis);
        maintain.setTainRemove("1");
        maintain.setTainSection("1");
        maintain.setTainSectionName("维修部");
        maintain.setTainPassword(pass);

        iBaseDao.save(maintain);
        iBaseDao.save(guard);

        return ZhongkaiResult.ok();


    }

    /**
     * 修改维修端用户
     *
     * @param uuid      用户id
     * @param gua_sex   性别
     * @param gua_name  姓名
     * @param gua_user  用户名
     * @param gua_phone 电话
     * @param gua_pass  密码
     * @param gua_state 状态 1在职 2请假 3离职
     */


    @Override
    public ZhongkaiResult updateGuard(String uuid, String gua_sex, String gua_name, String gua_user, String gua_phone, String gua_pass, String gua_state) {
        if (StringUtils.isNullOrEmpty(gua_sex) || StringUtils.isNullOrEmpty(gua_name)
                || StringUtils.isNullOrEmpty(gua_user) || StringUtils.isNullOrEmpty(gua_phone)
                || StringUtils.isNullOrEmpty(gua_pass) || StringUtils.isNullOrEmpty(gua_state)
                || StringUtils.isNullOrEmpty(uuid)
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Guard guard;
        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaUuid='" + uuid + "'");

        if (guard == null) {
            return ZhongkaiResult.build(400, "用户不存在");
        }

        if (!gua_phone.equals(guard.getGuaPhone())){
            Guard guardPhone = (Guard) iBaseDao.get("from Guard gd where gd.guaPhone='" + gua_phone + "'");

            if (guardPhone != null){
                return ZhongkaiResult.build(400, "此手机已被注册,请联系管理员");
            }
        }


        guard.setGuaSex(gua_sex);
        guard.setGuaName(gua_name);
        guard.setGuaUser(gua_user);
        guard.setGuaPhone(gua_phone);


        if (gua_pass.equals(guard.getGuaPass())){
            guard.setGuaPass(gua_pass);
        }else {
            //加密
            try {

                String pass = CryptoUtil.encode64(CryptoUtil.SALT, gua_pass);
                //  chinaset.setPassWord(pass); //设置加密后的密码
                guard.setGuaPass(pass);
            } catch (Exception e) {
                e.printStackTrace();
                return ZhongkaiResult.build(400, "数据加密失败");
            }
        }



        guard.setGuaState(gua_state);

        long millis = new DateTime().getMillis(); //获取当前系统时间戳

        guard.setGuaMoTime(millis);

        //.......................................

        Maintain maintain = (Maintain) iBaseDao.get("from Maintain ma where ma.tainUuid='" + uuid + "'");


        try {

            if (maintain != null) {
                maintain.setTainName(gua_name);
                maintain.setTainSex(gua_sex);
                maintain.setTainPhone(gua_phone);
                maintain.setTainStatus(gua_state);
                iBaseDao.update(maintain);
            }

            iBaseDao.update(guard);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(402, "修改失败");
        }

    }


    /**
     * 删除维修端用户
     *
     * @param uuid 用户标识
     */
    @Override
    public ZhongkaiResult removeGuard(String uuid) {
        if (StringUtils.isNullOrEmpty(uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Guard guard;
        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaUuid='" + uuid + "'");

        if (guard == null) {
            return ZhongkaiResult.build(400, "用户不存在");
        }

        try {
            //long millis = new DateTime().getMillis(); //获取当前系统时间戳

            //.................................
            Maintain maintain = (Maintain) iBaseDao.get("from Maintain ma where ma.tainUuid='" + uuid + "'");
            if (maintain != null) {
                iBaseDao.delete(maintain);
            }

            iBaseDao.delete(guard);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "删除失败");
        }

    }


    /**
     * 维修端列表
     *
     * @param page      当前页数
     * @param rows      每页数据条数
     * @param gua_state 用户状态      状态 1在职 2请假 3离职
     */
    @Override
    public ZhongkaiResult listGuard(Integer page, Integer rows, String gua_state) {
        if (page == 0 || rows == 0 || StringUtils.isNullOrEmpty(gua_state)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Guard.class);
            detachedCriteria.add(Restrictions.eq("guaRemove", "1")); //增加条件
            detachedCriteria.add(Restrictions.eq("guaState", gua_state)); //增加条件
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }


    }


    /**
     * 修改用户状态
     *
     * @param uuid      用户id
     * @param gua_state 用户状态 1在职 2请假 3离职
     */
    @Override
    public ZhongkaiResult leaveGuard(String uuid, String gua_state) {

        if (StringUtils.isNullOrEmpty(uuid) || StringUtils.isNullOrEmpty(gua_state)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Guard guard;
        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaUuid='" + uuid + "'");

        if (guard == null) {
            return ZhongkaiResult.build(400, "用户不存在");
        }

        try {
            long millis = new DateTime().getMillis(); //获取当前系统时间戳

            guard.setGuaMoTime(millis);
            guard.setGuaState(gua_state);

            //.....................................
            Maintain maintain = (Maintain) iBaseDao.get("from Maintain ma where ma.tainUuid='" + uuid + "'");
            if (maintain != null) {
                maintain.setTainStatus(gua_state);
                iBaseDao.update(maintain);
            }


            iBaseDao.update(guard);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "修改失败");
        }
    }
}
